class EventsController < ApplicationController

#  before_filter :login_required
  [:title, :director, :place].each {  |field|    in_place_edit_for :event, field }

#  caches_action :index, :show, :current_month

  # GET /events
  # GET /events.xml
  def index
    if params[:date]
      @events = Event.find(:all, :conditions => ['date = ?', params[:date]])
    elsif params[:month]
      @events = Event.find(:all, :conditions => "DATE_FORMAT(date, '%m') = #{params[:month]} ")
    else
      @events = Event.find(:all, :conditions => "DATE_FORMAT(date, '%m') = #{Time.now.month} ")
    end
    @all_events = Event.find(:all, :conditions => "DATE_FORMAT(date, '%m') = #{Time.now.month} ")

    respond_to do |format|
      format.html { if params[:nolayout] ; render :layout => false ; end }# index.rhtml
      format.xml  { render :xml => @events.to_xml }
    end
  end

  def show
    @event = params[:title] ? Event.find_by_title(params[:title]) : Event.find(params[:id])
    @all_events = Event.find :all
    respond_to do |format|
      format.html # show.rhtml
      format.xml  { render :xml => @event.to_xml }
      format.js { render :layout => false }
    end
  end

  # GET /events/new
  def new
    @event = Event.new
    @all_events = Event.find :all
  end

  # GET /events/1;edit
  def edit
    @event = Event.find(params[:id])
    @all_events = Event.find :all
  end

  # POST /events
  # POST /events.xml
  def create
    @event = Event.new(params[:event])
    @all_events = Event.find :all

    respond_to do |format|
      if @event.save
        flash[:notice] = 'Создано.'
        format.html { redirect_to event_url(@event) }
        format.xml  { head :created, :location => event_url(@event) }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @event.errors.to_xml }
      end
    end
  end

  # PUT /events/1
  # PUT /events/1.xml
  def update
    @event = Event.find(params[:id])

    respond_to do |format|
      if @event.update_attributes(params[:event])
        flash[:notice] = 'Обновлено.'
        format.html { redirect_back_or_default event_url(@event) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @event.errors.to_xml }
      end
    end
  end

  # DELETE /events/1
  # DELETE /events/1.xml
  def destroy
    @event = Event.find(params[:id])
    @event.comments.each { |c| c.destroy }
    @event.destroy

    respond_to do |format|
      format.html { redirect_to events_url }
      format.xml  { head :ok }
    end
  end

  def comment
    @comments = Event.find(params[:id]).comments
    @ncomments = @comments.count + 1
    @comment = @comments.create(params[:comment])
  end

  def current_month
    @events = Event.find(:all, :conditions => "DATE_FORMAT(date, '%m') = #{Time.now.month} ")
    render :layout => false if request.xhr?
  end

  def search
    if request.xhr?
      if params['search_text'].strip.length > 0
        terms = params['search_text'].split.collect do |word|
          "%#{word.downcase}%"
        end
        @events = Event.find(
                           :all,
                           :conditions => [
                                           ( ["(LOWER(title) LIKE ?)"] * terms.size ).join(" AND "),
                                           * terms.flatten
                                          ]
                           )
      end
      render :partial => "search"
    else
      redirect_to :action => "index"
    end
  end

end
